package kr.hkit.androidcomplete03.ch30;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class MyBroadcastReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = intent.getExtras(); // 메시지 수신 시 인텐트의 pdus번들로 전달
		SmsMessage[] msgs = null;
		StringBuilder str = new StringBuilder();
		if (bundle != null) {
			Object[] pdus = (Object[]) bundle.get("pdus");
			msgs = new SmsMessage[pdus.length];
			for (int i = 0; i < msgs.length; i++) {
				msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);// 번들의 메시지
																		// 추출
				str.append("SMS from " + msgs[i].getOriginatingAddress());
				str.append(" : ");
				str.append(msgs[i].getMessageBody().toString() + "\n");
			}
		}
		Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
	}

}
